/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.helpmanage.controller;

import com.icinfo.cloud.common.core.page.PageResponse;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.helpmanage.dto.AppHelpManageDto;
import com.icinfo.cloud.provider.punish.helpmanage.dto.AppHelpManageRequest;
import com.icinfo.cloud.provider.punish.helpmanage.dto.HelpManageDto;
import com.icinfo.cloud.provider.punish.helpmanage.dto.HelpManageUpdateDto;
import com.icinfo.cloud.provider.punish.helpmanage.service.IHelpManageService;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.web.BaseController;
import com.icinfo.framework.mybatis.mapper.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述:  help_manage 对应的Controller类.<br>
 *
 * @author framework generator
 * @date 2022年12月11日
 */
@RestController
@RefreshScope
@RequestMapping("/punish/ucase/help/manage")
@Slf4j
public class HelpManageController extends BaseController {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(HelpManageController.class);

    @Autowired
    private IHelpManageService helpManageService;

    /**
     * 描述：帮扶-新增/修改(传主键id) sourceType 0 个人 1 单位
     * @param manageDto
     * @return
     * @author wangfawei
     * @date 2022-12-11
     */
    @PostMapping("/save")
    @ApiOperation(value = "【V1.0.0】描述：帮扶-新增", httpMethod = "POST")
    @CrossOrigin
    public Result<String> save(@RequestBody @Validated HelpManageDto manageDto) {
        if(StringUtil.isEmpty(manageDto.getHelpUserName())){
            return  Result.error("帮扶对象姓名不为空");
        }
        if(StringUtil.isEmpty(manageDto.getHelpCerno())){
            return  Result.error("身份证件号码不为空");
        }
        if(StringUtil.isEmpty(manageDto.getHelpTel())){
            return  Result.error("联系方式不为空");
        }
        if(StringUtil.isEmpty(manageDto.getHelpResidentialAddress())){
            return  Result.error("居住地址不为空");
        }
        if (manageDto.getSourceType().equals(Constant.STRING_0)){
            if(StringUtil.isEmpty(manageDto.getHelpAge())){
                return  Result.error("年龄不为空");
            }
            if(StringUtil.isEmpty(manageDto.getHelpResidenceName())){
                return  Result.error("户籍性质不为空");
            }
            if(StringUtil.isEmpty(manageDto.getHelpType())){
                return  Result.error("帮扶类型不为空");
            }
        } else {
            if(StringUtil.isEmpty(manageDto.getDeptType())){
                return  Result.error("单位类型不为空");
            }
            if(StringUtil.isEmpty(manageDto.getLegalName())){
                return  Result.error("法人代表不为空");
            }
        }
        return helpManageService.save(manageDto);
    }

    /**
     * 描述：单位帮扶-修改(传主键id) sourceType 0 个人 1 单位
     * @param updateDto
     * @return
     * @author yehuafei
     * @date 2022-12-12
     */
    @PostMapping("/update")
    @ApiOperation(value = "【V1.0.0】描述：帮扶-新增", httpMethod = "POST")
    public Result<String> update(@RequestBody @Validated HelpManageUpdateDto updateDto) {
        return helpManageService.update(updateDto);
    }

    /**
     * 【V1.6.0】描述：帮扶列表 sourceType 0 个人 1 单位
     * @param manageDto 入参
     * @return List<HelpManageDto>
     * @author 王发魏
     * @date 2022-12-09
     **/
    @RequestMapping("/getHelpManageList")
    @ApiOperation(value = "【V1.0.0】描述：帮扶列表", httpMethod = "POST")
    @CrossOrigin
    public Result<PageResponse<HelpManageDto>> getHelpManageList(@RequestBody @Validated HelpManageDto manageDto) {
        System.out.println(System.currentTimeMillis());
        if(manageDto.getPageNum() == null){
            return Result.error("分页-每页页数不为空");
        }
        if(manageDto.getLength() == null){
            return Result.error("分页-每页大小不为空");
        }
        List<HelpManageDto> list = helpManageService.getHelpManageList(manageDto);
        return Result.success(Constant.STRING_GET_SUCCESS, new PageResponse<>(list));
    }

    /**
     * 【V1.6.0】描述：删除
     * @param id 入参
     * @return ResultString
     * @author yehuafei
     * @date 2022-12-12
     **/
    @GetMapping("/deleteById")
    @ApiOperation(value = "【V1.0.0】描述：帮扶列表", httpMethod = "GET")
    @CrossOrigin
    public Result<String> getHelpManageList(@RequestParam("id") String id) {
        int i = helpManageService.deleteById(id);
        if (i > 0){
            return Result.success(Constant.STRING_SUCCESS_EN);
        }
        return Result.error(Constant.STRING_FAIL_EN);
    }

    /**
     * 【V1.6.0】描述：APP帮扶对象查询
     * @param request 入参
     * @return ResultString
     * @author yehuafei
     * @date 2022-12-12
     **/
    @PostMapping("/getAppHelpManage")
    @ApiOperation(value = "【V1.0.0】描述：APP帮扶对象查询", httpMethod = "POST")
    public Result<AppHelpManageDto> getAppHelpManage(@RequestBody @Validated AppHelpManageRequest request) {
        if (request.getSourceType().equals(Constant.STRING_0)){
            if (request.getHelpCerno().length() != 15 && request.getHelpCerno().length() != 18){
                return Result.error("请输入正确的证件号码");
            }
        } else if (request.getSourceType().equals(Constant.STRING_1) && StringUtils.isBlank(request.getDeptType())){
            return Result.error("请选择单位类型");
        }
        AppHelpManageDto appHelpManage = helpManageService.getAppHelpManage(request);
        return Result.success(appHelpManage);
    }
}
